#include <REGX52.H>
#include "LCD1602.h"
#include "Delay.h"
#include "MatrixKey.h"

unsigned char KeyNum = 0;
unsigned int Password = 0;
unsigned char Count = 0;
void main(void)
{
	LCD_Init();
	LCD_ShowString(1,1,"Password:");
	while (1)
	{
		KeyNum = MatrixKey();
		if (KeyNum)
		{
			if (KeyNum <= 10 && Count < 4)//如果s1~s10按下，为输入密码
			{
				Password *= 10;			//密码左移一位
				Password += KeyNum % 10;//获取一位密码
				Count++;//计次加一
				LCD_ShowNum(2,1,Password,4);
			}
		}
		if (KeyNum == 11)//如果s11按下，确认
		{
			if (Password == 2345)
			{
				LCD_ShowString(1,14,"OK ");
				Password = 0;
				Count = 0;
				LCD_ShowNum(2,1,Password,4);
			}else{
				LCD_ShowString(1,14,"ERR");
				Password = 0;
				Count = 0;
				LCD_ShowNum(2,1,Password,4);
			}
		}
		if (KeyNum == 12)//如果s12按下，取消
		{
			Password = 0;
			Count = 0;
			LCD_ShowNum(2,1,Password,4);
		}
	}
}